package fun.gladlee.evensprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * @author gladlee
 *         Date: 13-5-15
 *         Time: 下午6:54
 */
public class EventsProvider extends ContentProvider {
    private static final String DBNAME = "EventsDatabase";
    private EventsDBHelper eventsDBHelper;
    private static final UriMatcher matcher = buildUriMatcher();

    private static final int EVENTS_TABLE=1;
    private static final int EVENTS_ROW=2;
    private static UriMatcher buildUriMatcher() {
        UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);
        um.addURI(EventsContract.AUTHORITY, EventsContract.Events.PATH, EVENTS_TABLE);
        um.addURI(EventsContract.AUTHORITY, EventsContract.Events.PATH+"/#", EVENTS_ROW);
        return um;
    }

    @Override
    public boolean onCreate() {
        eventsDBHelper = new EventsDBHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projections, String clause, String[] args, String order) {
        switch (matcher.match(uri)) {
            case EVENTS_TABLE:
                Cursor c = eventsDBHelper.getReadableDatabase().query(
                        EventsContract.Events.TABLE_NAME,
                        projections,
                        clause,
                        args,
                        null,
                        null,
                        order);
                return c;
            case EVENTS_ROW:
                break;
        }
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        return null;
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
        return 0;
    }

    protected final static class EventsDBHelper extends SQLiteOpenHelper {
        private SQLiteDatabase db;
        private static final int DB_VERSION = 2;

        private final static String EVENTS_TABLE_CREATE = String.format(
                "CREATE TABLE %s (%s INT PRIMARY KEY AUTOINCREMENT,%s TEXT,%s TEXT,%s INT);",
                EventsContract.Events.TABLE_NAME, BaseColumns._ID, EventsContract.Events.COL_SUBJECT,
                EventsContract.Events.COL_PLACE, EventsContract.Events.COL_DATE
        );

        public EventsDBHelper(Context context) {
            super(context, DBNAME, null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            db = sqLiteDatabase;
            db.execSQL(EVENTS_TABLE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
            db.execSQL("DROP TABLE IF EXISTS " + EventsContract.Events.TABLE_NAME);
            onCreate(db);
        }
    }

    ;
}
